﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ISRecord.View.Properties;
using System.Threading;
using System.Globalization;
//using WpfMessageBox = Microsoft.Windows.Controls.MessageBox;
namespace ISRecord.View.Services
{
    static class Helper
    {
        public static string ModuleName
        {
            get { return "ISRecord.View"; }
        }
        public static DateTime ParseDateString(string dateString)
        {

            string formattedDateString = PutMask(dateString);
            DateTime date;
            if (DateTime.TryParse(formattedDateString, out date))
            {
                return date;
            }
            return DateTime.Now;
        }

        public static string PutMask(string dateString)
        {
            if (!string.IsNullOrEmpty(dateString))
            {
                string y = dateString.Substring(0, 4);
                string m = dateString.Substring(4, 2);
                string d = dateString.Substring(6, 2);
                string f = y + "/" + m + "/" + d;
                return f;
            }
            return "";

        }

        public static void LogError(string errorMessage, string source)
        {
            string logFilePath = Settings.Default.LogFilePath;
            using (FileStream f = new FileStream(logFilePath, FileMode.Append, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(f))
                {
                    StringBuilder s = new StringBuilder();
                    s.AppendFormat("Error: {0} happend in {1} \tAt {2}", errorMessage, source, DateTime.Now);
                    writer.WriteLine(s.ToString());
                    writer.Flush();
                }
            }
        }

        public static bool ShowMessage(string msg, MessagePrompetType messageType)
        {
            switch (messageType)
            {
                case MessagePrompetType.AskQuestion:
                    if (MessageBox.Show(msg, "ISR", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes, MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading ) == MessageBoxResult.Yes)
                    {
                        return true;
                    }
                    break;
                case MessagePrompetType.ReportError:
                    MessageBox.Show(msg, "ISR", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading);
                    break;
                case MessagePrompetType.ReportInfo:
                    MessageBox.Show(msg, "ISR", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading);
                    break;

            }
            return false;
        }

        public static void SendKeyStroke(Control eventSource, Key keyToSend)
        {
            KeyEventArgs e = new KeyEventArgs(Keyboard.PrimaryDevice,
                                              Keyboard.PrimaryDevice.ActiveSource,
                                              0, keyToSend);
            e.RoutedEvent = Keyboard.KeyDownEvent;
            eventSource.RaiseEvent(e);
        }
        /// <summary>
        /// Validate the given date string according to Hijri calender rules
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsValidDate(string value)
        {

            if (value.Length != 8)
            {
                return false;
            }
            int temp;
            if (!int.TryParse(value, out temp))
            {
                return false;
            }

            int year = int.Parse(value.Substring(0, 4));
            int month = int.Parse(value.Substring(4, 2));
            int day = int.Parse(value.Substring(6, 2));
            if (day <= 0 || day > 30)
            {
                return false;
            }
            if (month <= 0 || month > 12)
            {
                return false;
            }
            if (year < 1380 || year > 1450)
            {
                return false;
            }
            return true;

        }
        /// <summary>
        /// Unmask the given date by removing backslash
        /// </summary>
        /// <param name="value"></param>
        /// <returns>Unmasked date strig</returns>
        /// <exception cref="FormatException"></exception>
        public static string UnmaskDate(string value)
        {
            if (value.Length != 12)
            {
                throw new FormatException("Date is not in recognized format");
            }
            string unmaskedDate;

            string y = value.Substring(value.Length - 4, 4);
            string m = value.Substring(value.Length - 8, 2);
            string d = value.Substring(0, 2);
            unmaskedDate = y + m + d;
            //}
            return unmaskedDate;
        }
        public static bool TryUnmaskDate(string value, out string result)
        {

            if (value.Length != 12)
            {
                result = null;
                return false;
            }

            string y = value.Substring(value.Length - 4, 4);
            string m = value.Substring(value.Length - 8, 2);
            string d = value.Substring(0, 2);
            string unmaskedDate = y + m + d;
            result = unmaskedDate;
            return true;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        /// <exception cref="FormatException"></exception>
        public static string DateMiror(string s)
        {
            if (s.Length == 10)
            {

                return new StringBuilder().Append(s.Substring(s.Length - 2, 2))
                                           .Append("/")
                                           .Append(s.Substring(s.Length - 5, 2))
                                           .Append("/")
                                           .Append(s.Substring(0, 4))
                                           .ToString();
            }
            if (s.Length == 8)
            {
                return new StringBuilder().Append(s.Substring(s.Length - 2, 2))
                    .Append("/")
                    .Append(s.Substring(s.Length - 4, 2))
                    .Append("/")
                    .Append(s.Substring(0, 4))
                    .ToString();
            }
            throw new FormatException("صيغة التاريخ خاطئة");
        }


        public static bool ConfirmDelete()
        {
            string msg = Resources.ConfirmDeleteMsg;
            return ShowMessage(msg, MessagePrompetType.AskQuestion);
        }


        //internal static string GetTodayDateString()
        //{
        //    Thread orignalThread = Thread.CurrentThread;
        //    CultureInfo orignalCulture = orignalThread.CurrentCulture;
        //    CultureInfo saudiCulture = new CultureInfo("ar-SA");
        //    if (orignalThread.CurrentCulture != saudiCulture)
        //    {
        //        orignalThread.CurrentCulture = saudiCulture;
        //    }
        //    string todayDate = DateTime.Today.t
             
        //}

        
    }
}
